/*
 * Copyright 2011 PA Consulting Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prodeagle.java.servlets;

import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.prodeagle.java.counters.BatchCounter;

public class RandomCounters extends HttpServlet {
	/**
	 * Auto-generated for Serializable
	 */
	private static final long serialVersionUID = -6993461086859610850L;
	
	private static final Logger _logger = Logger.getLogger(RandomCounters.class.getName());
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) {
		_logger.info("Randomly generating values for counters");
		
		double random = Math.random();
		
		int numberOfCounters = (int) (random * 100);
		
		_logger.info("Number of counters to randomly increment: " + numberOfCounters);
		
		BatchCounter batchCounters = new BatchCounter();
		
		while (numberOfCounters > 0) {
			batchCounters.increment("Counter" + numberOfCounters);
			numberOfCounters--;
		}
		
		batchCounters.commit();
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		doGet(req, resp);
	}
}
